package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.IotEntranceURIDaoI;
import sy.dao.IotVideoURIDaoI;
import sy.model.IotEntranceURI;
import sy.model.IotVideoURI;
@Repository("iotVideoURIDao")
public class IotVideoURIDaoImpl implements IotVideoURIDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(IotVideoURI iotVideoURI) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(iotVideoURI);
	}

	@Override
	public void update(IotVideoURI iotVideoURI) {
		// TODO Auto-generated method stub
		this.getCurrentSession().saveOrUpdate(iotVideoURI);
	}

	@Override
	public void delete(List<Integer> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from IotVideoURI where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public List<IotVideoURI> list() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from IotVideoURI").list();
	}

	@Override
	public List<IotVideoURI> listIotVideoURIByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from IotVideoURI where uri like :key or uniqueCode like :key or name like :key";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		return query.list();
	}

	@Override
	public IotVideoURI getIotVideoURIById(Integer id) {
		// TODO Auto-generated method stub
		String hql = "from IotVideoURI where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameter("id", id);
		List<IotVideoURI> list = query.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
}
